//
//  TjianTableViewCell.m
//  NBTripe
//
//  Created by 陈曦 on 16/1/12.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "TjianTableViewCell.h"
#import "TjianCollectionViewCell.h"

@interface TjianTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
@end

@implementation TjianTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCollectionView];
        
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    self.collectionView.frame = CGRectMake(10,  0, self.width - 20, (self.collectionView.width / 2 ) * (self.detailModel.hottest_sitesArray.count / 2 +_detailModel.hottest_sitesArray.count % 2));
    // item的大小
    self.flowLayout.itemSize = CGSizeMake(self.collectionView.width / 2 - 5, self.collectionView.width / 2 - 5);
    
    // item的最小行间距
    _flowLayout.minimumLineSpacing = 10;
    
    // item的最小列间距
    _flowLayout.minimumInteritemSpacing = 10;
    
}
#pragma mark - 赋值
-(void)setDetailModel:(DetailedFirstModel *)detailModel {
    if (_detailModel != detailModel) {
        _detailModel = detailModel;
    }
    [self.collectionView reloadData];
}
#pragma mark - create CollectionView

- (void)createCollectionView {
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    // 滑动方向
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor colorWithRed:250/255.0 green:246/255.0 blue:232/255.0 alpha:1];
    [self.collectionView registerNib:[UINib nibWithNibName:@"TjianCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"TjianCollectionViewCell"];
    self.collectionView.scrollEnabled = NO;
    [self addSubview:self.collectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.detailModel.hottest_sitesArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TjianCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TjianCollectionViewCell" forIndexPath:indexPath];
    cell.hoModel = self.detailModel.hottest_sitesArray[indexPath.row];
    
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  
    NSString *str = (NSString *)[self.detailModel.hottest_sitesArray[indexPath.row] url];
    self.webGo([deURL stringByAppendingString:str]);
    
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
